/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.entity.Price;
import org.hutech09.jsale.entity.Product;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Nam
 */
public class LazyPriceDataModel extends LazyDataModel<Price> {

    private IProductManager pm;

    private Product product;

    public LazyPriceDataModel(IProductManager pm, Product product) {
        this.pm = pm;
        this.product = product;
    }

    @Override
    public Object getRowKey(Price object) {
        return object.getId();
    }

    @Override
    public Price getRowData(String rowKey) {
        return pm.findPriceById(Long.parseLong(rowKey));
    }

    @Override
    public List<Price> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        List<Price> result;
        if (product != null) {
            myFilter.put("product", product);
        }
        if (sortOrder == SortOrder.DESCENDING) {
            result = pm.findPricePage(first, pageSize, sortField, true, myFilter);
        } else {
            result = pm.findPricePage(first, pageSize, sortField, false, myFilter);
        }
        setRowCount((int) pm.countPrice(myFilter));
        return result;
    }
}
